package top.kangyaocoding.ltzf.payment.nativepay;

import retrofit2.Call;
import retrofit2.Response;
import top.kangyaocoding.ltzf.factory.Configuration;
import top.kangyaocoding.ltzf.payment.nativepay.model.*;

import java.io.IOException;

/**
 * @Author K·Herbert
 * @Description 扫码支付
 * @Date 2024-05-14 16:34
 */

public class NativePayService {
    private final Configuration configuration;
    private final INativePayApi nativePayApi;

    public NativePayService(Configuration configuration, INativePayApi nativePayApi) {
        this.configuration = configuration;
        this.nativePayApi = nativePayApi;
    }

    public PrepayResponse prePay(PrepayRequest request) throws IOException {
        Call<PrepayResponse> call = nativePayApi.prePay(
                request.getMchId(),
                request.getOutTradeNo(),
                request.getTotalFee(),
                request.getBody(),
                request.getTimestamp(),
                request.getNotifyUrl(),
                request.getAttach(),
                request.getTimeExpire(),
                request.getDeveloperAppId(),
                request.createSign(configuration.getPartnerKey()));
        // 获取数据
        Response<PrepayResponse> response = call.execute();
        return response.body();
    }

    /*
     * 查询订单
     *
     * @param request 请求入参
     * @return 订单信息
     * @throws Exception 异常
     */
    public QueryOrderByOutTradeNoResponse queryOrderByOutTradeNo(QueryOrderByOutTradeNoRequest request) throws IOException {
        // 1. 请求接口 & 签名
        Call<QueryOrderByOutTradeNoResponse> call = nativePayApi.getPayOrder(
                request.getMchId(),
                request.getOutTradeNo(),
                request.getTimestamp(),
                request.createSign(configuration.getPartnerKey())
        );
        // 2. 获取数据
        Response<QueryOrderByOutTradeNoResponse> response = call.execute();
        // 3. 返回结果
        return response.body();
    }

    /*
     * 发起退单
     *
     * @param request 退单信息
     * @return 退单结果
     * @throws Exception 异常
     */
    public RefundOrderResponse refundOrder(RefundOrderRequest request) throws IOException {
        // 1. 请求接口 & 签名
        Call<RefundOrderResponse> call = nativePayApi.refundOrder(
                request.getMchId(),
                request.getOutTradeNo(),
                request.getOutRefundNo(),
                request.getTimestamp(),
                request.getRefundFee(),
                request.getRefundDesc(),
                request.getNotifyUrl(),
                request.createSign(configuration.getPartnerKey())
        );
        // 2. 获取数据
        retrofit2.Response<RefundOrderResponse> execute = call.execute();
        // 3. 返回结果
        return execute.body();
    }

    /*
     * 查询退单结果
     * @param request 请求参数
     * @return 退单信息
     * @throws Exception 异常
     */
    public GetRefundOrderResponse getRefundOrder(GetRefundOrderRequest request) throws IOException {
        // 1. 请求接口 & 签名
        Call<GetRefundOrderResponse> call = nativePayApi.getRefundOrder(
                request.getMchId(),
                request.getOutRefundNo(),
                request.getTimestamp(),
                request.createSign(configuration.getPartnerKey())
        );
        // 2. 获取数据
        retrofit2.Response<GetRefundOrderResponse> execute = call.execute();
        // 3. 返回结果
        return execute.body();
    }
}
